/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "LUA_Panel.h"
#include "../gui/MPanel.h"
#include "../gui/GUI_Manager.h"
#include "../kernel.h"

extern Kernel* g_kernel;

int LUA_Panel::Create(lua_State *L)
{
    // retrieve the name from lua
    std::string name   = luaL_checkstring( L, 1 );
    // retrieve the potential owner
    std::string sowner = luaL_checkstring( L, 2 );
    const gui::Component* owner = gui::GUI_Manager::GetInstance()->GetParentComponent( sowner );
    
    m_comp = new gui::MPanel( owner ); // might be NULL
    m_comp->SetName( name );
    if ( !owner )
        gui::GUI_Manager::GetInstance()->AddComponent( m_comp );

    return 0;
}

LUA_Panel::~LUA_Panel(void)
{
}


/** Add a Background Image */
int LUA_Panel::AddBackgroundImage( lua_State *L )
{
    if ( m_comp )
    {
        gui::MPanel* p = static_cast<gui::MPanel*>( m_comp );
        // retrieve the name of the image from lua
        std::string image   = luaL_checkstring( L, 1 );

        p->AddBackgroundImage ( g_kernel->GetResource()->LoadSingleImage( image ) );
    }
    return 0;
}

/** Set FPS */
int LUA_Panel::SetFPS( lua_State *L )
{
    if ( m_comp )
    {
        gui::MPanel* p = static_cast<gui::MPanel*>( m_comp );
        // retrieve the fps from lua
        int fps   = luaL_checknumber( L, 1 );

        p->SetFPS( fps );
    }
    return 0;
}


// -- Bind to LUA
#define method(class, name) {#name, &class::##name}

const char LUA_Panel::className[] = "GUI_Panel";
Lunar<LUA_Panel>::RegType LUA_Panel::methods[] = {
        method(LUA_Panel, Create),
        method(LUA_Panel, Destroy),
        method(LUA_Panel, SetName),
        method(LUA_Panel, GetName),
        method(LUA_Panel, SetCaption),
        method(LUA_Panel, GetCaption),
        method(LUA_Panel, SetLocation),
        method(LUA_Panel, GetLocation),
        method(LUA_Panel, SetSize),
        method(LUA_Panel, GetWidth),
        method(LUA_Panel, GetHeight),
        method(LUA_Panel, SetBackgroundColor),
        method(LUA_Panel, GetBackgroundColor),
        method(LUA_Panel, SetAlpha),
        method(LUA_Panel, GetAlpha),
        method(LUA_Panel, SetFocus),
        method(LUA_Panel, HasFocus),
        method(LUA_Panel, SetVisible),
        method(LUA_Panel, IsVisible),
        method(LUA_Panel, SetForegroundColor),
        method(LUA_Panel, GetForegroundColor),

        method(LUA_Panel, AddBackgroundImage),
        method(LUA_Panel, SetFPS),
        {NULL, NULL}
};